package com.ffcs.crmd.tsp.task.job.impl.test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import com.ffcs.crmd.tsp.api.dto.task.TspJobDataMap;
import com.ffcs.crmd.tsp.api.dto.task.TspJobExecutionException;
import com.ffcs.crmd.tsp.api.dto.task.TspJobInstance;
import com.ffcs.crmd.tsp.api.task.job.IJobBatch;
import com.ffcs.crmd.tsp.api.task.job.IJobExecutionContext;

public class ThreadPoolJobRandomImpl implements IJobBatch<Object> {
    
    private static AtomicLong index        = new AtomicLong();
    
    private TspJobInstance    tspJobDetail = null;
    
    @Override
    public void execute(IJobExecutionContext<Object> context) throws TspJobExecutionException {
        tspJobDetail = context.getTspJobInstance();
        System.out.println(context.getTspJobInstance().getJobKey() + " --- count" + index.incrementAndGet());
        List<Object> businessDatas = new ArrayList<Object>();
        TspJobDataMap tspJobDataMap = context.getTspJobDataMap();
        businessDatas.addAll(tspJobDataMap.getMap().values());
        context.joinBussinessDatas(businessDatas);
    }
    
    @Override
    public void businessProcess(Object param) throws TspJobExecutionException {
        long random = (long) (Math.random() * 10000);
        System.out.println(Thread.currentThread().getName() + "---" + tspJobDetail.getName() + "--- data:" + param + " ---waiting time:" + random);
        try {
            Thread.sleep(random);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
